home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / presto / prest_04.lha / src / shmalloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-08  |  22.5 KB  |  862 lines

  1. /*
  2.  * This memory allocation code is derived from the GNU memory allocator.
  3.  * Please read the FSF copyright notice at the end of this file.
  4.  */
  5.  
  6. /*
  7.  * @(#)nmalloc.c 1 (Caltech) 2/21/82
  8.  *
  9.  *    U of M Modified: 20 Jun 1983 ACT: strange hacks for Emacs
  10.  *
  11.  *    Nov 1983, Mike@BRL, Added support for 4.1C/4.2 BSD.
  12.  *
  13.  * This is a very fast storage allocator.  It allocates blocks of a small 
  14.  * number of different sizes, and keeps free lists of each size.  Blocks
  15.  * that don't exactly fit are passed up to the next larger size.  In this 
  16.  * implementation, the available sizes are (2^n)-4 (or -16) bytes long.
  17.  * This is designed for use in a program that uses vast quantities of
  18.  * memory, but bombs when it runs out.  To make it a little better, it
  19.  * warns the user when he starts to get near the end.
  20.  *
  21.  * June 84, ACT: modified rcheck code to check the range given to malloc,
  22.  * rather than the range determined by the 2-power used.
  23.  *
  24.  * Jan 85, RMS: calls malloc_warning to issue warning on nearly full.
  25.  * No longer Emacs-specific; can serve as all-purpose malloc for GNU.
  26.  * You should call malloc_init to reinitialize after loading dumped Emacs.
  27.  * Call malloc_stats to get info on memory stats if MSTATS turned on.
  28.  * realloc knows how to return same block given, just changing its size,
  29.  * if the power of 2 is correct.
  30.  *
  31.  * May 88, Univ. of Washington: Significant (but relatively simple)
  32.  * changes to make this code work in a Sequent shared-memory/PRESTO
  33.  * environment.
  34.  *
  35.  * Perhaps the biggest difference is that whenever we have to ask for
  36.  * more space, we get a BIG chunk rather than a relatively small one.
  37.  * Another change is that requests for memory >= the system page size
  38.  * will return memory that is aligned to a page boundary, due to the
  39.  * prevalent use of mmap(2).
  40.  *
  41.  * June 89, Univ. of Washington: Worked on memory alignment bug in malloc().
  42.  *
  43.  * Most of the changes are bracketed by the SHARED and PRESTO $ifdef's.
  44.  *
  45.  */
  46.  
  47.  
  48. #define SHARED
  49. #define PRESTO
  50.  
  51. #include <sys/types.h>
  52.  
  53. #ifdef SHARED
  54.  
  55. #ifdef sequent
  56. #include <parallel/parallel.h>
  57. #define sbrk shsbrk
  58. #endif sequent
  59.  
  60. #ifdef sun
  61. #  define shared /**/
  62. #  define private /**/
  63.    typedef    int slock_t;
  64. #  define L_UNLOCKED 0
  65. #  ifdef PRESTO
  66. #      define S_LOCK s_lock
  67. #      define S_UNLOCK s_unlock
  68. #  endif PRESTO
  69. #endif sun
  70.  
  71. #ifdef vax
  72. #define shared /**/
  73. #define private /**/
  74. typedef    int slock_t;
  75. #define L_UNLOCKED 0
  76.  
  77. #ifdef PRESTO
  78. /*
  79.  * Redefine S_LOCK and S_UNLOCK to be s_lock() and s_unlock(),
  80.  * included in vax_lock.s in Presto source.  Should probably be
  81.  * asm macros.  On the vax, the shared allocation code won't ld
  82.  * outside of a presto environment.
  83.  */
  84. #define S_LOCK s_lock
  85. #define S_UNLOCK s_unlock
  86. #else
  87. XXX
  88. #endif PRESTO
  89. #endif vax
  90.  
  91. #ifdef PRESTO
  92. /*
  93.  * See comment on naming of the memory allocator in misc.c.
  94.  *
  95.  * #define malloc shmalloc
  96.  * #define free shfree
  97.  */
  98.  
  99. #endif PRESTO
  100.  
  101. #define mstats shmstats
  102. #define realloc shrealloc
  103.     
  104. #endif SHARED
  105.  
  106. #ifdef PRESTO
  107. /*
  108.  * These external routines are needed to avoid a deadlock situation where
  109.  * we preempt a thread controlling access to the memory management 
  110.  * stuff.  Preempting while holding onto this stuff could make things
  111.  * go very very slowly.
  112.  */
  113.  
  114. extern void enable_interrupts();
  115. extern int disable_interrupts();
  116. #endif PRESTO
  117.  
  118.  
  119. /*
  120.  * Determine which kind of system this is.
  121.  */
  122. #define BSD42
  123. #define BSD
  124. #include <sys/time.h>
  125. #include <sys/resource.h>
  126.  
  127. #define ISALLOC ((char) 0xf7)    /* magic byte that implies allocation */
  128. #define ISFREE ((char) 0x54)    /* magic byte that implies free block */
  129.                 /* this is for error checking only */
  130. #define ISMEMALIGN ((char) 0xd6)  /* Stored before the value returned by
  131.                      memalign, with the rest of the word
  132.                      being the distance to the true
  133.                      beginning of the block.  */
  134.  
  135. /*
  136.  * NBINS == number of "headers" pointing to blocks of size 2**(n+3).
  137.  */
  138. #define NBINS 30
  139.  
  140. /*
  141.  * If MSTATS is defined then
  142.  * nmalloc[i] is the difference between the number of mallocs and frees
  143.  * for a given block size.
  144.  */
  145.  
  146. #ifdef MSTATS
  147. #ifdef SHARED
  148. shared static int nmalloc[NBINS];
  149. shared static int nmal, nfre;
  150. #else
  151. static int nmalloc[NBINS];
  152. static int nmal, nfre;
  153. #endif
  154. #endif /* MSTATS */
  155.  
  156. /*
  157.  * If range checking is not turned on, all we have is a flag indicating
  158.  * whether memory is allocated, an index in nextf[], and a size field; to
  159.  * realloc() memory we copy either size bytes or 1<<(index+3) bytes depending
  160.  * on whether the former can hold the exact size (given the value of
  161.  * 'index').  If range checking is on, we always need to know how much space
  162.  * is allocated, so the 'size' field is only used to remember memory alignment
  163.  */
  164.  
  165. struct mhead {
  166.     char     mh_alloc;    /* ISALLOC or ISFREE */
  167.     char     mh_index;    /* index in nextf[] */
  168. /* Remainder are valid only when block is allocated */
  169.     unsigned short mh_size;    /* size, if < 0x10000 */
  170. #ifdef rcheck
  171.     unsigned mh_nbytes;    /* number of bytes allocated */
  172.     int      mh_magic4;    /* should be == MAGIC4 */
  173. #endif /* rcheck */
  174. };
  175.  
  176. /*
  177.  * Access free-list pointer of a block.
  178.  * It is stored at block + 4.
  179.  * This is not a field in the mhead structure
  180.  * because we want sizeof (struct mhead)
  181.  * to describe the overhead for when the block is in use,
  182.  * and we do not want the free-list pointer to count in that.
  183.  */
  184.  
  185. #define CHAIN(a) \
  186.   (*(struct mhead **) (sizeof (char *) + (char *) (a)))
  187.  
  188. #ifdef rcheck
  189.  
  190. /*
  191.  * To implement range checking, we write magic values in at the beginning and
  192.  * end of each allocated block, and make sure they are undisturbed whenever a
  193.  * free or a realloc occurs.
  194.  */
  195.  
  196. /* Written in each of the 4 bytes following the block's real space */
  197. #define MAGIC1 0x55
  198.  
  199. /* Written in the 4 bytes before the block's real space */
  200. #define MAGIC4 0x55555555
  201.  
  202. #define ASSERT(p) if (!(p)) botch("p"); else
  203. #define EXTRA  4        /* 4 bytes extra for MAGIC1s */
  204.  
  205. #else
  206.  
  207. #define ASSERT(p)
  208. #define EXTRA  0
  209.  
  210. #endif /* rcheck */
  211.  
  212. /*
  213.  * nextf[i] is the pointer to the next free block of size 2^(i+3).  The
  214.  * smallest allocatable block is 8 bytes.  The overhead information will
  215.  * go in the first int of the block, and the returned pointer will point
  216.  * to the second.
  217.  *
  218.  * busy[i] is meant to indicate that a recursive call to malloc
  219.  * has been made (e.g., via a signal handler). Not used in this
  220.  * (May 88) version.
  221.  */
  222.  
  223. #ifdef SHARED
  224. /* nextf[i] is free list of blocks of size 2**(i + 3)  */
  225. shared static struct mhead *nextf[NBINS];
  226.  
  227. /* busy[i] is nonzero while allocation of block size i is in progress.  */
  228. shared static char busy[NBINS];
  229.  
  230. /* Number of bytes of writable memory we can expect to be able to get */
  231. shared static unsigned int lim_data;
  232.  
  233. /* nonzero once initial bunch of free blocks made */
  234. shared static int gotpool;
  235.  
  236. /* Mutex for structure access */
  237. shared static slock_t lock;
  238.  
  239. /* System page size in bytes */
  240. shared static int syspgsize;
  241.  
  242. #ifdef PRESTO
  243.  
  244. #define BEGIN_CRITICAL {        \
  245.     disable_interrupts();        \
  246.     S_LOCK(&lock);            \
  247. }
  248.  
  249. #define END_CRITICAL {            \
  250.     S_UNLOCK(&lock);        \
  251.     enable_interrupts();        \
  252. }
  253.  
  254. #else
  255.  
  256. #define BEGIN_CRITICAL {        \
  257.     S_LOCK(&lock);            \
  258. }
  259.  
  260. #define END_CRITICAL {            \
  261.     S_UNLOCK(&lock);        \
  262. }
  263.  
  264. #endif PRESTO
  265.  
  266. #else    /* not shared */
  267.  
  268. #ifdef PRESTO
  269.  
  270. #define BEGIN_CRITICAL disable_interrupts();
  271. #define END_CRITICAL enable_interrupts();
  272.  
  273. #else
  274.  
  275. #define BEGIN_CRITICAL ;
  276. #define END_CRITICAL ;
  277.  
  278. #endif PRESTO
  279.  
  280. /* nextf[i] is free list of blocks of size 2**(i + 3)  */
  281. static struct mhead *nextf[NBINS];
  282.  
  283. /* busy[i] is nonzero while allocation of block size i is in progress.  */
  284. static char busy[NBINS];
  285.  
  286. /* Number of bytes of writable memory we can expect to be able to get */
  287. static unsigned int lim_data;
  288.  
  289. /* nonzero once initial bunch of free blocks made */
  290. static int gotpool;
  291.  
  292. /* System page size in bytes */
  293. static int syspgsize;
  294.  
  295. #endif SHARED
  296.  
  297. char *grab_hunk();
  298.  
  299. /*
  300.  * BRKSIZELOG2 is log2-3 of the minimum chunk of memory we will ask for
  301.  * if we have to request more memory (ie, via sbrk).  In our case we ask for
  302.  * the maximum that will fit in the mh_size field, 65K.
  303.  */
  304. #define BRKSIZELOG2 13
  305.  
  306. static morecore (nu)            /* ask system for more memory */
  307.     register int nu;        /* size index to get more of  */
  308. {
  309.     register char *cp;
  310.     register int nblks;
  311.     register unsigned int siz;
  312.  
  313.  /*
  314.   * On initial startup, get one block of each size up to some reasonable
  315.   * size.
  316.   */
  317.   if (!gotpool) {
  318.     getpool (); 
  319.     gotpool = 1;
  320.     if ( nextf[nu] ) return; /* Getpool got it for us */
  321.    }
  322.  
  323.  /*
  324.   * Must be either a big block or we are really out of memory.
  325.   */
  326.   nblks = 1;
  327.   if ((siz = nu) < BRKSIZELOG2)
  328.     nblks = 1 << ((siz = BRKSIZELOG2) - nu);
  329.  
  330.   if ((cp = grab_hunk(1 << (siz + 3))) == (char *) -1)
  331.     return;            /* no more room! */
  332.  
  333.   if ((int) cp & 7) {
  334.     /*
  335.      * shouldn't happen, but just in case
  336.      */
  337.     cp = (char *) (((int) cp + 8) & ~7);
  338.     nblks--;
  339.   }
  340.  
  341.   /*
  342.    * save new header and link the nblks blocks together
  343.    */
  344.   nextf[nu] = (struct mhead  *) cp;
  345.   siz = 1 << (nu + 3);
  346.   while (1) {
  347.     ((struct mhead *) cp) -> mh_alloc = ISFREE;
  348.     ((struct mhead *) cp) -> mh_index = nu;
  349.     if (--nblks <= 0)
  350.     break;
  351.     CHAIN ((struct mhead   *) cp) = (struct mhead  *) (cp + siz);
  352.     cp += siz;
  353.   }
  354.   CHAIN ((struct mhead   *) cp) = 0;
  355. }
  356.  
  357. /*
  358.  * Get a chunk of memory from the system.  Make sure the pointer that
  359.  * we return is just sizeof(struct mhead) bytes shy of a page boundary.
  360.  * Calls to this function must be serialized.
  361.  */
  362.  
  363. char *
  364. grab_hunk(nbytes)
  365.   int nbytes;
  366. {
  367.   char *cp;
  368.   char *sbrk ();
  369.  
  370.   cp = sbrk(0);
  371.   /*
  372.    * land on pagesize boundaries 
  373.    */
  374.   if (((int) cp + sizeof(struct mhead)) & (syspgsize-1)) {
  375.     sbrk (syspgsize - (((int) cp + sizeof( struct mhead)) & (syspgsize-1)));
  376.   }
  377.  
  378.   return(sbrk(nbytes));
  379. }
  380.  
  381. /*
  382.  * Get a chunk of memory from the system.  Make sure the pointer that
  383.  * we return is just on a page boundary.  This memory should **not**
  384.  * be used by the memory allocator.
  385.  */
  386.  
  387. char *
  388. grab_nonmalloc_hunk(nbytes)
  389.   int nbytes;
  390. {
  391.   char *cp;
  392.   char *sbrk ();
  393.  
  394.   BEGIN_CRITICAL;
  395.   cp = sbrk(0);
  396.   /*
  397.    * land on pagesize boundaries 
  398.    */
  399.   if (((int) cp) & (syspgsize-1)) {
  400.     sbrk (syspgsize - (((int) cp) & (syspgsize-1)));
  401.   }
  402.  
  403.   cp = sbrk(nbytes);
  404.   END_CRITICAL;
  405.   return cp;
  406. }
  407.  
  408. static getpool ()
  409. {
  410.   register int nu;
  411.   register char *cp;
  412.   int logpgsz;
  413.   
  414.   /* Get 16*pagesize of storage */
  415.   cp = grab_hunk(16*syspgsize);
  416.   if (cp == (char *) -1)
  417.     return;
  418.  
  419.   /*
  420.   /* Divide it into an initial 8-word block
  421.    * plus one block of size 2**nu for nu = 3 ... log2(chunksize*pgsize)-1.
  422.    */
  423.   logpgsz = 8*syspgsize;
  424.   nu = 0;
  425.   while ( ! (logpgsz & 1) ) {
  426.       logpgsz >>= 1;
  427.       nu++;
  428.   }
  429.   logpgsz = nu - 3;
  430.   
  431.   CHAIN (cp) = nextf[0];
  432.   nextf[0] = (struct mhead *) cp;
  433.   ((struct mhead *) cp) -> mh_alloc = ISFREE;
  434.   ((struct mhead *) cp) -> mh_index = 0;
  435.   cp += 8;
  436.  
  437.   for (nu = 0; nu < logpgsz; nu++)
  438.     {
  439.       CHAIN (cp) = nextf[nu];
  440.       nextf[nu] = (struct mhead *) cp;
  441.       ((struct mhead *) cp) -> mh_alloc = ISFREE;
  442.       ((struct mhead *) cp) -> mh_index = nu;
  443.       cp += 8 << nu;
  444.     }
  445. }
  446.  
  447. char *malloc (n)        /* get a block */
  448.      unsigned n;
  449. {
  450.   register struct mhead *p;
  451.   register unsigned int nbytes;
  452.   register int nunits = 0;
  453.   int mustalign;
  454.   char *aligned;
  455.   struct mhead *p2;
  456.   
  457.   /*
  458.    * Figure out how many bytes are required, rounding up to the nearest
  459.    * multiple of 4, then figure out which nextf[] area to use.
  460.    */
  461.   if ( !syspgsize ) {
  462.       syspgsize = getpagesize();
  463.   }
  464.  
  465.   /*
  466.    * If asking for >= syspgsize bytes, make sure
  467.    * the return value is page aligned in case of mmap(2).
  468.    */
  469.   if ( n >= syspgsize ) {
  470.     mustalign = 1;
  471.       n += syspgsize;
  472.   }
  473.   else {
  474.     mustalign = 0;
  475.   }
  476.     
  477.   nbytes = (n + sizeof *p + EXTRA + 3) & ~3;
  478.   {
  479.     register unsigned int   shiftr = (nbytes - 1) >> 2;
  480.  
  481.     while (shiftr >>= 1)
  482.       nunits++;
  483.   }
  484.  
  485.   BEGIN_CRITICAL;
  486.  
  487.   /* If there are no blocks of the appropriate size, go get some */
  488.   /* COULD SPLIT UP A LARGER BLOCK HERE ... ACT */
  489.   if (nextf[nunits] == 0)
  490.     morecore (nunits);
  491.  
  492.   /* Get one block off the list, and set the new list head */
  493.   if ((p = nextf[nunits]) == 0) {
  494.       END_CRITICAL;
  495.       return 0;
  496.   }
  497.  
  498.   nextf[nunits] = CHAIN (p);
  499.  
  500.   END_CRITICAL;
  501.  
  502.   /*
  503.    * Check for free block clobbered
  504.    * If not for this check, we would gobble a clobbered free chain ptr
  505.    * and bomb out on the NEXT allocate of this size block
  506.    */
  507.   if (p -> mh_alloc != ISFREE || p -> mh_index != nunits)
  508. #ifdef rcheck
  509.     botch ("block on free list clobbered");
  510. #else /* not rcheck */
  511.     abort ();
  512. #endif /* not rcheck */
  513.  
  514.   /* Fill in the info, and if range checking, set up the magic numbers */
  515.   p -> mh_alloc = ISALLOC;
  516.  
  517. #ifdef rcheck
  518.   p -> mh_nbytes = n;
  519.   p -> mh_magic4 = MAGIC4;
  520.   {
  521.     register char  *m = (char *) (p + 1) + n;
  522.  
  523.     *m++ = MAGIC1, *m++ = MAGIC1, *m++ = MAGIC1, *m = MAGIC1;
  524.   }
  525. #else /* not rcheck */
  526.   p -> mh_size = n;
  527. #endif /* not rcheck */
  528.  
  529. #ifdef MSTATS
  530.   nmalloc[nunits]++;
  531.   nmal++;
  532. #endif /* MSTATS */
  533.  
  534.   if ( mustalign ) {
  535.       if ( ((int)(p+1) & (syspgsize-1)) != 0 ) {
  536.       aligned = (char *) (p+1);
  537.       aligned = (char *) (((int)aligned + syspgsize - 1) & -syspgsize);
  538.       p2 = (struct mhead *) aligned - 1;
  539.       p2->mh_size = aligned - (char *)( p + 1 );
  540.       p2->mh_alloc = ISMEMALIGN;
  541.       return aligned;
  542.       }
  543.   }
  544.   
  545.   return (char *) (p + 1);
  546. }
  547.  
  548. free (mem)
  549.      char *mem;
  550. {
  551.   register struct mhead *p;
  552.  
  553.   {
  554.     register char *ap = mem;
  555.  
  556.     if (ap == 0)
  557.       return;
  558.  
  559.     p = (struct mhead *) ap - 1;
  560.     if (p -> mh_alloc == ISMEMALIGN)
  561.       {
  562.     ap -= p->mh_size;
  563.     p = (struct mhead *) ap - 1;
  564.       }
  565.  
  566.     if (p -> mh_alloc != ISALLOC)
  567. #ifdef rcheck
  568.       abort ();
  569. #else
  570.       return;
  571. #endif
  572. #ifdef rcheck
  573.     ASSERT (p -> mh_magic4 == MAGIC4);
  574.     ap += p -> mh_nbytes;
  575.     ASSERT (*ap++ == MAGIC1); ASSERT (*ap++ == MAGIC1);
  576.     ASSERT (*ap++ == MAGIC1); ASSERT (*ap   == MAGIC1);
  577. #endif /* rcheck */
  578.  
  579.   }
  580.  
  581.   {
  582.     register int nunits = p -> mh_index;
  583.  
  584.     ASSERT (nunits <= (NBINS-1));
  585.     p -> mh_alloc = ISFREE;
  586.  
  587.     BEGIN_CRITICAL;
  588.  
  589.     /* Put this block on the free list.  */
  590.     CHAIN (p) = nextf[nunits];
  591.     nextf[nunits] = p;
  592.  
  593. #ifdef MSTATS
  594.     nmalloc[nunits]--;
  595.     nfre++;
  596. #endif /* MSTATS */
  597.  
  598.     END_CRITICAL;
  599.   }
  600. }
  601.  
  602.  
  603. #ifdef MSTATS
  604. /*
  605.  * Return statistics describing allocation of blocks of size 2**n.
  606.  *
  607.  * This seems like a foolish way to do things, but who am I to argue
  608.  * with Stallman, et al.
  609.  */
  610.  
  611. struct mstats_value {
  612.     int blocksize;
  613.     int nfree;
  614.     int nused;
  615. };
  616.  
  617. struct mstats_value malloc_stats (size)
  618.      int size;
  619. {
  620.   struct mstats_value v;
  621.   register int i;
  622.   register struct mhead *p;
  623.  
  624.   v.nfree = 0;
  625.  
  626.   if (size < 0 || size >= NBINS)
  627.     {
  628.       v.blocksize = 0;
  629.       v.nused = 0;
  630.       return v;
  631.     }
  632.  
  633.   BEGIN_CRITICAL;
  634.   v.blocksize = 1 << (size + 3);
  635.   v.nused = nmalloc[size];
  636.   for (p = nextf[size]; p; p = CHAIN (p))
  637.     v.nfree++;
  638.   END_CRITICAL;
  639.  
  640.   return v;
  641. }
  642. #endif /* MSTATS */
  643.  
  644. char *realloc (mem, n)
  645.     char *mem;
  646.     register unsigned n;
  647. {
  648.   register struct mhead *p;
  649.   register unsigned int tocopy;
  650.   register unsigned int nbytes;
  651.   register int nunits;
  652.  
  653.   if ((p = (struct mhead *) mem) == 0)
  654.     return malloc (n);
  655.   p--;
  656.   nunits = p -> mh_index;
  657.   ASSERT (p -> mh_alloc == ISALLOC);
  658.  
  659. #ifdef rcheck
  660.   ASSERT (p -> mh_magic4 == MAGIC4);
  661.   {
  662.     register char *m = mem + (tocopy = p -> mh_nbytes);
  663.     ASSERT (*m++ == MAGIC1); ASSERT (*m++ == MAGIC1);
  664.     ASSERT (*m++ == MAGIC1); ASSERT (*m   == MAGIC1);
  665.   }
  666. #else /* not rcheck */
  667.   if (p -> mh_index >= 13)
  668.     tocopy = (1 << (p -> mh_index + 3)) - sizeof *p;
  669.   else
  670.     tocopy = p -> mh_size;
  671. #endif /* not rcheck */
  672.  
  673.   /* See if desired size rounds to same power of 2 as actual size. */
  674.   nbytes = (n + sizeof *p + EXTRA + 7) & ~7;
  675.  
  676.   /* If ok, use the same block, just marking its size as changed.  */
  677.   if (nbytes > (4 << nunits) && nbytes <= (8 << nunits))
  678.     {
  679. #ifdef rcheck
  680.       register char *m = mem + tocopy;
  681.       *m++ = 0;  *m++ = 0;  *m++ = 0;  *m++ = 0;
  682.       p-> mh_nbytes = n;
  683.       m = mem + n;
  684.       *m++ = MAGIC1;  *m++ = MAGIC1;  *m++ = MAGIC1;  *m++ = MAGIC1;
  685. #else /* not rcheck */
  686.       p -> mh_size = n;
  687. #endif /* not rcheck */
  688.       return mem;
  689.     }
  690.  
  691.   if (n < tocopy)
  692.     tocopy = n;
  693.   {
  694.     register char *new;
  695.  
  696.     if ((new = malloc (n)) == 0)
  697.       return 0;
  698.     bcopy (mem, new, tocopy);
  699.     free (mem);
  700.     return new;
  701.   }
  702. }
  703.  
  704. /*
  705.  * The following routines are not used and are left for documentation
  706.  * purposes only.
  707.  */
  708.  
  709. #ifdef NOTDEF
  710. char *memalign ( alignment, size)
  711.      unsigned alignment, size;
  712. {
  713.   register char *ptr = malloc (size + alignment);
  714.   register char *aligned;
  715.   register struct mhead *p;
  716.  
  717.   if (ptr == 0)
  718.     return 0;
  719.   /*
  720.    * If entire block has the desired alignment, just accept it.
  721.    */
  722.   if (((int) ptr & (alignment - 1)) == 0)
  723.     return ptr;
  724.   /* Otherwise, get address of byte in the block that has that alignment.  */
  725.   aligned = (char *) (((int) ptr + alignment - 1) & -alignment);
  726.   /* Store a suitable indication of how to free the block,
  727.      so that free can find the true beginning of it.  */
  728.   p = (struct mhead *) aligned - 1;
  729.   p -> mh_size = aligned - ptr;
  730.   p -> mh_alloc = ISMEMALIGN;
  731.   return aligned;
  732. }
  733.  
  734. /* This runs into trouble with getpagesize on HPUX.
  735.    Patching out seems cleaner than the ugly fix needed.
  736. char *
  737. valloc (size)
  738. {
  739.   return memalign (getpagesize (), size);
  740. }
  741. */
  742.  
  743. get_lim_data ()
  744. {
  745.   struct rlimit XXrlimit;
  746.  
  747.   getrlimit (RLIMIT_DATA, &XXrlimit);
  748. #ifdef RLIM_INFINITY
  749.   lim_data = XXrlimit.rlim_cur & RLIM_INFINITY; /* soft limit */
  750. #else
  751.   lim_data = XXrlimit.rlim_cur;    /* soft limit */
  752. #endif
  753. }
  754.  
  755. #endif NOTDEF
  756.  
  757.  
  758.  
  759. /* dynamic memory allocation for GNU.
  760.    Copyright (C) 1985, 1987 Free Software Foundation, Inc.
  761.  
  762.                NO WARRANTY
  763.  
  764.   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  765. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  766. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  767. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  768. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  769. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  770. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  771. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  772. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  773. CORRECTION.
  774.  
  775.  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  776. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  777. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  778. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  779. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  780. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  781. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  782. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  783. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  784. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  785.  
  786.         GENERAL PUBLIC LICENSE TO COPY
  787.  
  788.   1. You may copy and distribute verbatim copies of this source file
  789. as you receive it, in any medium, provided that you conspicuously and
  790. appropriately publish on each copy a valid copyright notice "Copyright
  791. (C) 1985 Free Software Foundation, Inc."; and include following the
  792. copyright notice a verbatim copy of the above disclaimer of warranty
  793. and of this License.  You may charge a distribution fee for the
  794. physical act of transferring a copy.
  795.  
  796.   2. You may modify your copy or copies of this source file or
  797. any portion of it, and copy and distribute such modifications under
  798. the terms of Paragraph 1 above, provided that you also do the following:
  799.  
  800.     a) cause the modified files to carry prominent notices stating
  801.     that you changed the files and the date of any change; and
  802.  
  803.     b) cause the whole of any work that you distribute or publish,
  804.     that in whole or in part contains or is a derivative of this
  805.     program or any part thereof, to be licensed at no charge to all
  806.     third parties on terms identical to those contained in this
  807.     License Agreement (except that you may choose to grant more extensive
  808.     warranty protection to some or all third parties, at your option).
  809.  
  810.     c) You may charge a distribution fee for the physical act of
  811.     transferring a copy, and you may at your option offer warranty
  812.     protection in exchange for a fee.
  813.  
  814. Mere aggregation of another unrelated program with this program (or its
  815. derivative) on a volume of a storage or distribution medium does not bring
  816. the other program under the scope of these terms.
  817.  
  818.   3. You may copy and distribute this program (or a portion or derivative
  819. of it, under Paragraph 2) in object code or executable form under the terms
  820. of Paragraphs 1 and 2 above provided that you also do one of the following:
  821.  
  822.     a) accompany it with the complete corresponding machine-readable
  823.     source code, which must be distributed under the terms of
  824.     Paragraphs 1 and 2 above; or,
  825.  
  826.     b) accompany it with a written offer, valid for at least three
  827.     years, to give any third party free (except for a nominal
  828.     shipping charge) a complete machine-readable copy of the
  829.     corresponding source code, to be distributed under the terms of
  830.     Paragraphs 1 and 2 above; or,
  831.  
  832.     c) accompany it with the information you received as to where the
  833.     corresponding source code may be obtained.  (This alternative is
  834.     allowed only for noncommercial distribution and only if you
  835.     received the program in object code or executable form alone.)
  836.  
  837. For an executable file, complete source code means all the source code for
  838. all modules it contains; but, as a special exception, it need not include
  839. source code for modules which are standard libraries that accompany the
  840. operating system on which the executable file runs.
  841.  
  842.   4. You may not copy, sublicense, distribute or transfer this program
  843. except as expressly provided under this License Agreement.  Any attempt
  844. otherwise to copy, sublicense, distribute or transfer this program is void and
  845. your rights to use the program under this License agreement shall be
  846. automatically terminated.  However, parties who have received computer
  847. software programs from you with this License Agreement will not have
  848. their licenses terminated so long as such parties remain in full compliance.
  849.  
  850.   5. If you wish to incorporate parts of this program into other free
  851. programs whose distribution conditions are different, write to the Free
  852. Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  853. worked out a simple rule that can be stated here, but we will often permit
  854. this.  We will be guided by the two goals of preserving the free status of
  855. all derivatives of our free software and of promoting the sharing and reuse of
  856. software.
  857.  
  858.  
  859. In other words, you are welcome to use, share and improve this program.
  860. You are forbidden to forbid anyone else to use, share and improve
  861. what you give them.   Help stamp out software-hoarding!  */
  862.